use salvo::{handler, Request};
use crate::domain::dto::{rbatis_to_res, PageRes, Res, ResObj};
use crate::domain::entity::biz_entity::BizEntity;
use crate::domain::error::AccountError;
use crate::service::biz_service;

/// biz_pages 业务线分页
#[handler]
pub async fn biz_pages(request: &mut Request) -> Res<PageRes<BizEntity>> {
    let page = request.query::<u64>("page").unwrap_or(1);
    let limit = request.query::<u64>("limit").unwrap_or(10);
    let name = request.query::<String>("name");
    let result = biz_service::biz_page(page, limit, name).await;
    rbatis_to_res(result)
}

/// biz_add 业务线添加
#[handler]
pub async fn biz_add(request: &mut Request) -> Res<bool> {
    let name = request.query::<String>("name");
    let order = request.query::<u64>("order").unwrap_or(0);
    if name.is_none() {
        return Err(ResObj::fail_with_error(AccountError::RequestError));
    }
    let result = biz_service::biz_create(name.unwrap(), order).await;
    rbatis_to_res(result)
}

/// group_update 业务线更新
#[handler]
pub async fn biz_update(request: &mut Request) -> Res<bool> {
    let id = request.query::<u64>("id").unwrap_or(0);
    let name = request.query::<String>("name");
    let order = request.query::<u64>("order").unwrap_or(0);
    if name.is_none() || id == 0 {
        return Err(ResObj::fail_with_error(AccountError::RequestError));
    }
    let result = biz_service::biz_update(id, name.unwrap(), order).await;
    rbatis_to_res(result)
}

/// group_delete 业务线删除
#[handler]
pub async fn biz_delete(request: &mut Request) -> Res<bool> {
    let id_option = request.query::<u64>("id");
    if id_option.is_none() {
        return Err(ResObj::fail_with_error(AccountError::RequestError));
    }
    let res = biz_service::biz_delete(id_option.unwrap()).await;
    rbatis_to_res(res)
}